﻿Imports System.Configuration
Imports System.Xml

Namespace Config
    ''' <summary>
    ''' </summary>
    Public Class ExceptionSectionHandler
        Implements IConfigurationSectionHandler

        Public Function Create(ByVal parent As Object, ByVal configContext As Object, ByVal section As XmlNode) As Object Implements System.Configuration.IConfigurationSectionHandler.Create
            Dim configEx As New ExceptionConfig()


            For Each attr As XmlAttribute In section.Attributes
                Select Case attr.Name
                    Case "ErrorCodeSource"
                        If True Then
                            configEx.ErrorCodeSource = DirectCast([Enum].Parse(GetType(ErrorCodeSource), attr.Value), ErrorCodeSource)
                            Exit Select
                        End If
                    Case "ConnectionString"
                        If True Then
                            configEx.ConnectionString = attr.Value
                            Exit Select
                        End If
                    Case "DataTable"
                        If True Then
                            configEx.DataTable = attr.Value
                            Exit Select
                        End If
                    Case Else
                        If True Then
                            Throw New ConfigurationErrorsException("識別することが出来ません", attr)
                        End If
                End Select
            Next

            For Each node As XmlNode In section.ChildNodes
                Dim exHandler As New ExceptionConfig.ExceptionHandlerConfig()

                If node.Name <> "ExHandler" Then
                    Throw New ConfigurationErrorsException("識別することが出来ません", node)
                Else
                    For Each attr As XmlAttribute In node.Attributes
                        Select Case attr.Name
                            Case "Name"
                                If True Then
                                    exHandler.Name = attr.Value
                                    Exit Select
                                End If
                            Case "ReturnMode"
                                If True Then
                                    exHandler.ReturnMode = DirectCast([Enum].Parse(GetType(ExceptionReturnMode), attr.Value), ExceptionReturnMode)
                                    Exit Select
                                End If
                            Case "AlertType"
                                If True Then
                                    exHandler.AlertType = DirectCast([Enum].Parse(GetType(ExceptionAlertType), attr.Value), ExceptionAlertType)
                                    Exit Select
                                End If
                            Case "Type"
                                If True Then
                                    exHandler.Type = attr.Value
                                    Exit Select
                                End If
                            Case Else
                                If True Then
                                    Throw New ConfigurationErrorsException("識別することが出来ません", attr)
                                End If
                        End Select
                    Next

                    exHandler.LogConfigColletion = New ExceptionConfig.LogHandlerConfigCollection()

                    For Each childNodeNode As XmlNode In node.ChildNodes
                        If childNodeNode.Name <> "LogHandler" Then
                            Throw New ConfigurationErrorsException("識別することが出来ません", childNodeNode)
                        Else

                            Dim logConfig As New ExceptionConfig.LogHandlerConfig()
                            For Each attr As XmlAttribute In childNodeNode.Attributes
                                Select Case attr.Name
                                    Case "Type"
                                        If True Then
                                            logConfig.Type = attr.Value
                                            Exit Select
                                        End If
                                    Case "ConnectionString"
                                        If True Then
                                            logConfig.ConnectionString = attr.Value
                                            Exit Select
                                        End If
                                    Case "DataTable"
                                        If True Then
                                            logConfig.DataTable = attr.Value
                                            Exit Select
                                        End If
                                    Case Else
                                        If True Then
                                            Throw New ConfigurationErrorsException("識別することが出来ません", attr)
                                        End If
                                End Select
                            Next
                            exHandler.LogConfigColletion.Add(logConfig)
                        End If
                    Next
                End If
                configEx.Add(exHandler)
            Next
            Return configEx
        End Function

    End Class
End Namespace
